package com.example.xunai.service.system.impl;

import com.example.xunai.service.system.SystemHealthService;
import com.example.xunai.service.cache.CacheService;
import com.example.xunai.service.ai.ModelConfigService;
import com.example.xunai.service.ai.strategy.AiModelFactory;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class SystemHealthServiceImpl implements SystemHealthService {

    private final ModelConfigService modelConfigService;
    private final CacheService cacheService;
    private final AiModelFactory aiModelFactory;

    /**
     * 系统健康检查
     */
    @Override
    public Map<String, Object> healthCheck() {
        Map<String, Object> health = new HashMap<>();
        health.put("service", "AI Chat System");
        health.put("status", "UP");
        health.put("timestamp", System.currentTimeMillis());

        // 模型配置健康检查
        Map<String, Object> modelHealth = modelConfigService.modelHealthCheck();
        health.put("modelConfig", modelHealth);

        // 缓存健康检查
        Map<String, Object> cacheHealth = cacheService.getCacheStats();
        health.put("cache", cacheHealth);

        // 系统信息
        Map<String, Object> systemInfo = new HashMap<>();
        systemInfo.put("availableProcessors", Runtime.getRuntime().availableProcessors());
        systemInfo.put("freeMemory", Runtime.getRuntime().freeMemory());
        systemInfo.put("totalMemory", Runtime.getRuntime().totalMemory());
        systemInfo.put("maxMemory", Runtime.getRuntime().maxMemory());
        health.put("system", systemInfo);

        return health;
    }

    /**
     * 详细健康检查
     */
    @Override
    public Map<String, Object> detailedHealthCheck() {
        Map<String, Object> detailedHealth = healthCheck();

        // 添加更多详细检查...
        detailedHealth.put("detailedCheck", true);
        detailedHealth.put("cacheService", "RedisCacheService");
        detailedHealth.put("streamService", "StreamChatService");
        detailedHealth.put("syncService", "SyncChatService");

        return detailedHealth;
    }
}